import { compose, createStore } from "redux";
import rootReducer from "./reducers/rootReducer";
import { loadState, saveState } from "./localstorage";

const persistedState = loadState();
const store = createStore(
  rootReducer,
  persistedState,
  compose(
    window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__()
  )
);

store.subscribe(() => {
  saveState({
    chats: store.getState().chats,
    account: store.getState().account,
  });
});

export default store;
